package dao;

import model.Conn;
import model.Type;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class TypeDao {
    PreparedStatement ps = null;
    ResultSet rs = null;
    Connection conn1 = null;
    Conn conn2 = new Conn();

    /**
     * 获取所有type表的数据
     *
     * @return list<Type> 返回一个集合
     * @throws SQLException
     */
    public List<Type> getTypeList() throws SQLException {
        String sql = "select * from type";
        conn1 = conn2.getConn();
        ps = conn1.prepareStatement(sql);
        rs = ps.executeQuery();
        List<Type> typeList = new ArrayList<>();
        while (rs.next()) {
            Type type = new Type();
            type.setId(rs.getInt(1));
            type.setName(rs.getString(2));
            typeList.add(type);
        }
        Conn.ClossAll(conn1, ps, rs);
        return typeList;
    }

    /**
     * 添加类目
     *
     * @param typeName 类目名
     * @return boolean
     * @throws SQLException
     */
    public boolean addType(String typeName) throws SQLException {
        String sql = "INSERT INTO `cakeshop`.`type` (`name`) VALUES (?)";
        conn1 = conn2.getConn();
        ps = conn1.prepareStatement(sql);
        ps.setString(1, typeName);
        int i = ps.executeUpdate();
        Conn.ClossAll(conn1, ps, rs);
        return i > 0;
    }

    /**
     * 查找数据库里是否已有相同的名
     *
     * @param typeName 类目名
     * @return boolean
     * @throws SQLException
     */
    public boolean hasTypeName(String typeName) throws SQLException {
        String sql = "SELECT * FROM type WHERE type.`name` = ?";
        conn1 = conn2.getConn();
        ps = conn1.prepareStatement(sql);
        ps.setString(1, typeName);
        rs = ps.executeQuery();
        boolean flag = rs.next();
        Conn.ClossAll(conn1, ps, rs);
        return flag;
    }

    /**
     * 通过id更新type数据
     *
     * @param id       要修改名字的typeId
     * @param typeName 要修改的名
     * @return boolean
     * @throws SQLException
     */
    public boolean updataTypeName(int id, String typeName) throws SQLException {
        String sql = " UPDATE `cakeshop`.`type` SET `name` = ? WHERE `id` = ?";
        conn1 = conn2.getConn();
        ps = conn1.prepareStatement(sql);
        ps.setString(1, typeName);
        ps.setInt(2, id);
        int i = ps.executeUpdate();
        Conn.ClossAll(conn1, ps, rs);
        return i > 0;
    }

    /**
     * 删除指定的type类目
     *
     * @param id type类目的id
     * @return boolean
     * @throws SQLException
     */
    public boolean deleteTypeById(int id) throws SQLException {
        String sql = " DELETE FROM `cakeshop`.`type` WHERE `id` = ?";
        conn1 = conn2.getConn();
        ps = conn1.prepareStatement(sql);
        ps.setInt(1, id);
        int i = ps.executeUpdate();
        Conn.ClossAll(conn1, ps, rs);
        return i > 0;
    }

    /**
     * 获取type名通过id
     *
     * @param id id
     * @return 名字||null
     * @throws SQLException
     */
    public Type getTypeNameById(int id) throws SQLException {
        String sql = "SELECT * FROM `cakeshop`.`type` WHERE id = ?";
        conn1 = conn2.getConn();
        ps = conn1.prepareStatement(sql);
        ps.setInt(1, id);
        rs = ps.executeQuery();
        Type type = null;
        if (rs.next()) {
            type = new Type();
            type.setId(rs.getInt("id"));
            type.setName(rs.getString("name"));
        }
        Conn.ClossAll(conn1, ps, rs);
        return type;
    }
}
